package com.novanok.micurso.entidad;

import android.app.Activity;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.novanok.micurso.CursosAlumnoActivity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

public class Alumno implements Serializable{

    private String id;
    private String email;
    private String nombre;
    private String legajo;
    private ArrayList<Curso> misCursos;
    private Activity activity;

    public Alumno(){
        this.misCursos = new ArrayList<Curso>();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getLegajo() {
        return legajo;
    }

    public void setLegajo(String legajo) {
        this.legajo = legajo;
    }

    public ArrayList<Curso> getMisCursos() {
        return misCursos;
    }

    public void cargarMisCursos(){
        Firebase rootRef = new Firebase("https://mi-curso.firebaseio.com/alumno/" + this.getId() + "/mis_cursos");
        rootRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();

                Curso curso = new Curso();

                curso.setId(dataSnapshot.getKey());
                curso.setCurso_id(newPost.get("curso_id").toString());
                curso.setNombre(newPost.get("nombre").toString());
                curso.setCatedra(newPost.get("catedra").toString());
                curso.setAula(newPost.get("aula").toString());
                curso.setHorario(newPost.get("horario").toString());

                curso.escucharNotificaciones();

                Alumno.this.getMisCursos().add(curso);

                if (Alumno.this.activity != null && Alumno.this.activity instanceof CursosAlumnoActivity){
                    ((CursosAlumnoActivity)Alumno.this.activity).cargarMisCursos();
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }


    public Curso devolverCursoPorCursoId(String curso_id){
        for (Curso curso : this.getMisCursos()){
            if (curso.getCurso_id().compareTo(curso_id) == 0)
                return curso;
        }
        return null;
    }

    public void setCursoAlumnoActivity(Activity activity){
        this.activity = activity;
        for (Curso curso : this.getMisCursos()){
            curso.setActivity(activity);
        }
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
